﻿using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using QuanLyKhachSan.Validation;
using DataAccess;
using System;

namespace QuanLyKhachSan
{
    public class DichVuManager:IValidator<DichVu>
    {
        public ValidateResult Validate(DichVu obj)
        {
            var validateResult = new ValidateResult();
            if (obj.Gia <= 0){
                validateResult.Message.Add("Đơn giá phải lớn hơn 0");
            }
            if (obj.TenDichVu.Equals(""))
            {
                validateResult.Message.Add("Tên dịch vụ bắt buộc nhập");
            }
            return validateResult;
        }

        public ActionResult Delete(DichVu obj)
        {
            return Delete(obj.MaDV);
        }

        public ActionResult Delete(int servicesId)
        {
            var actionResult = new ActionResult();
            try
            {
                using (var context = new QLKhachSanContext())
                {
                    using (DbContextTransaction dbTransaction = context.Database.BeginTransaction())
                    {
                        try
                        {
                            context.Database.ExecuteSqlCommand(@"delete DichVu where MaDV = " + servicesId);
                            dbTransaction.Commit();
                        }
                        catch (Exception ex)
                        {
                            dbTransaction.Rollback();
                            actionResult.Exceptions.Add(ex);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                actionResult.Exceptions.Add(ex);
            }
            return actionResult;
        }

        public ActionResult Add(DichVu obj)
        {
            var acResult = new ActionResult();
            try
            {
                using (var context  = new QLKhachSanContext())
                {
                    var dichVu = context.DichVus.SingleOrDefault(dv => dv.TenDichVu.ToLower().Equals(obj.TenDichVu.ToLower()));
                    if (dichVu == null)
                    {
                        context.DichVus.Add(obj);
                        context.SaveChanges();
                    }
                    else
                        acResult.Exceptions.Add(new Exception("Dịch vụ này đã tồn tại"));
                }
            }
            catch (Exception ex)
            {
                acResult.Exceptions.Add(ex);
            }
            return acResult;
        }

        public ActionResult InsertOrUpdate(DichVu obj)
        {
            var actionResult = new ActionResult();
            try
            {
                using (var context = new QLKhachSanContext())
                {
                    try
                    {
                        var serv = context.DichVus.FirstOrDefault(dv => dv.MaDV.Equals(obj.MaDV));
                        if (serv != null){
                            //update
                            serv.TenDichVu = obj.TenDichVu;
                            serv.Gia = obj.Gia;
                            serv.TrangThai = obj.TrangThai;
                            serv.DonViTinh = obj.DonViTinh;
                            serv.GiamGia = obj.GiamGia;
                            context.Entry(serv).State = EntityState.Modified;
                        }else
                        {
                            //add
                            context.DichVus.Add(obj);
                        }
                        context.SaveChanges();
                    }
                    catch (Exception ex)
                    {
                        actionResult.Exceptions.Add(ex);
                    }
                }
            }
            catch (Exception ex)
            {
                actionResult.Exceptions.Add(ex);
            }
            return actionResult;
        }

        public ActionResultAsList<DichVu> GetAllServices()
        {
            var actionResult = new ActionResultAsList<DichVu>();
            try
            {
                var context = new QLKhachSanContext();
                actionResult.Result =context.DichVus.ToList();
            }
            catch (Exception ex)
            {
                actionResult.Exceptions.Add(ex);
            }
            return actionResult;
        }

        public ActionResultAsList<DichVu> Find(Expression<Func<DichVu, bool>> predicate)
        {
            var acResult = new ActionResultAsList<DichVu>();
            try
            {
                using (var context = new QLKhachSanContext())
                {
                    acResult.Result = context.DichVus.Where(predicate).ToList();
                    if(!acResult.Result.Any())
                        acResult.Exceptions.Add(new Exception("Không tìm thấy dịch vụ thỏa điều kiện"));
                }
            }
            catch (Exception ex)
            {
                acResult.Exceptions.Add(ex);
            }
            return acResult;
        }

        public List<String> GetUnitServices(){
            List<string> listUnit;
            using (var context = new QLKhachSanContext())
            {
                listUnit = context.DichVus.Select(d => d.DonViTinh).ToList();
            }
            return listUnit;
        } 
    }
}
